package com.audiocn.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import com.audiocn.Utils.Log;
import com.audiocn.Utils.Utils;
import com.audiocn.dc.BaseDC;
import com.audiocn.dc.PosterBuyDC;
import com.audiocn.dc.PosterDC;
import com.audiocn.dc.PostersChanelsListDC;
import com.audiocn.dc.PostersDC;
import com.audiocn.engine.PostersEngine;
import com.audiocn.model.BaoBeiDescModel;
import com.audiocn.model.PosterModel;
import com.audiocn.poster.Application;
import com.audiocn.poster.Configs;
import com.audiocn.poster.MsgListDC;
import com.audiocn.poster.R;

/**
 * 画报模块manager
 */
public class PostersManager extends BaseManager {

	PostersEngine engine = null;
	PosterDC posterDC = null;
	PostersDC postersDC = null;
	PostersChanelsListDC pclDC = null;
	PosterBuyDC pbdc = null;
	MsgListDC mld = null;
	public static Map<String, String> imgs = null;
	public static Map<String, String> imgs_posters = null;

	public static String USER_POSTERS_CHANEL = "user_posters_chanel";

	public static int POSITION = 0;

	public String url = null;

	int floor = 1;
	int position = 0;
	int flag = 1;
	private int page = 0;

	BaoBeiDescModel model = null;

	public PostersManager(Context c) {
		super(c);
		engine = new PostersEngine(this, c);
		imgs = new HashMap<String, String>();
		imgs_posters = new HashMap<String, String>();
	}

	@SuppressWarnings("unchecked")
	@Override
	public void handleMessage(Message msg) {
		int flag = msg.what;
		switch (flag) {
		case 0:// 动态的更新画报列表的图片显示
			if (postersDC != null) {
				PostersDC.isNotify = true;
				PostersDC.isFirst = false;
				this.postersDC.getAdapter().notifyDataSetChanged();
				if (msg.arg1 == 2 && msg.arg2 == 3) {
					PostersDC.isNotify = false;
					PostersDC.isFirst = true;
				}
			}
			break;
		case 2:// 当初始化好画报详情列表数据后,更新画报详情
			if (posterDC != null) {
				posterDC.dismissDialog();
				if (msg.obj != null
						&& ((List<PosterModel>) msg.obj).size() != 0) {
					posterDC.setData((List<PosterModel>) msg.obj);
					this.posterDC.getPosterAdapter().notifyDataSetChanged();
				} else {
					posterDC.showTipDialog("获取画报列表失败");
				}
			}
			break;
		case 3:// 动态获取画报详情的图片后,更新画报列表
			if (posterDC != null) {
				PosterDC.isNotify = true;
				PosterDC.isFirst = false;
				this.posterDC.getPosterAdapter().notifyDataSetChanged();
				posterDC.dismissGettingImage();
				if (msg.arg1 == 2) {
					PosterDC.isNotify = false;
					PosterDC.isFirst = true;
				}
			}
			break;
		case 4:// 获取到画报列表后更新画报列表
			if (this.postersDC != null) {
				this.postersDC.dismissDialog();
				if (null != postersDC.getPosters()
						&& postersDC.getPosters().size() != 0) {
					this.postersDC.getAdapter().notifyDataSetChanged();
				} else {
					postersDC.showTipDialog("获取画报列表失败");
				}
			}

			break;
		case 5:// 当点击画报列表中的某个图片后进入画报详情列表
			if (!Utils.isNetworkValidate() && postersDC != null) {
				this.showNetworkError(postersDC);
				return;
			}
			floor = 2;
			posterDC = new PosterDC(context,
					Application.getLayoutId(R.layout.poster_list), this);
			enterSubDC(posterDC);
			position = msg.arg1;
			break;
		case 6:
			if (posterDC != null) {
				posterDC.mGalleryAdapter.notifyDataSetChanged();
			}
			break;
		case 7:
			if (posterDC != null) {
				posterDC.mAdapter.notifyDataSetChanged();
				posterDC.lv.invalidate();
			}
			break;
		case 8:
			if (!Utils.isNetworkValidate() && posterDC != null) {
				this.showNetworkError(posterDC);
				return;
			}
			if (msg.obj != null) {
				model = (BaoBeiDescModel) msg.obj;
				floor = 4;
				if (pbdc == null) {
					pbdc = new PosterBuyDC(context,
							Application.getLayoutId(R.layout.poster_buy), this);
				}
				enterSubDC(pbdc);
			}
			break;
		case 9:
			if (!Utils.isNetworkValidate() && postersDC != null) {
				this.showNetworkError(postersDC);
				return;
			}
			Log.e("9", "9");
			floor = 3;
			if (pclDC == null) {
				pclDC = new PostersChanelsListDC(context,
						Application.getLayoutId(R.layout.posters_chanels), this);
			}
			enterSubDC(pclDC);
			break;
		case 10:
			if (pclDC != null) {
				if (pclDC.getData() == null || pclDC.getData().size() == 0) {
					Toast.makeText(context, "获取信息失败,请重试..", 0).show();
				}
				pclDC.listView.setVisibility(View.VISIBLE);
				pclDC.adapter.notifyDataSetChanged();
				pclDC.dismissDialog();
			}
			break;
		case 11:
			if (postersDC != null) {
				this.flag = 2;
				Log.e("11", "11");
				floor = 3;
				page = 0;
				if (postersDC.title.getText().toString() != null
						&& postersDC.title
								.getText()
								.toString()
								.trim()
								.equals(context.getSharedPreferences(
										PostersManager.USER_POSTERS_CHANEL,
										Context.MODE_APPEND).getString(
										"chanel_name", "女人"))) {
					this.flag = 1;
				} else {
					postersDC.getData().clear();
					postersDC.getAdapter().notifyDataSetChanged();
					postersDC.dismissProgress();
				}
				postersDC.title.setText(context
						.getSharedPreferences(
								PostersManager.USER_POSTERS_CHANEL,
								Context.MODE_APPEND).getString("chanel_name",
								"女人"));
				back();
			}
			break;
		case 12:
			if (!Utils.isNetworkValidate()) {
				return;
			}
			if (postersDC != null) {
				if (msg.arg1 == postersDC.sign) {
					engine.getThePositionPosters(msg.arg1,
							postersDC.getPosters());
				}
			}
			break;
		case 13:
			if (!Utils.isNetworkValidate()) {
				return;
			}
			if (posterDC != null) {
				if (msg.arg1 == posterDC.sign) {
					engine.getThePositionPoster(msg.arg1, posterDC.getData());
				}
			}
			break;
		case 14:
			onEnterEnd();
			break;
		case 15:
			BaoBeiDescModel model = null;
			if (msg.obj != null) {
				model = (BaoBeiDescModel) msg.obj;
				String url = model.getClick_url();
				if (url != null) {
					if (url.endsWith(".jhtml")) {
						url = url + "?sid=" + Configs.TAOBAO_SID_DEFAULT;
					} else {
						url = url + "&sid=" + Configs.TAOBAO_SID_DEFAULT;
					}
					url = url + "&imsi=" + Utils.getIMSI();
					url = url + "&imei=" + Utils.getIMEI();
					url = url + "&ttid=" + Configs.ttid;
					pbdc = new PosterBuyDC(context,
							Application.getLayoutId(R.layout.poster_buy), this);
					pbdc.loadURL(url);
				}
			}
			break;
		case 110:
			Log.e("110", "110");
			floor = msg.arg1;
			back();
			break;
		case 120:
			floor = 0;
			break;
		case 119:

			break;
		case 130:
			if (floor == 1) {
				freePosters();
				floor = 0;
			} else if (floor == 2) {
				freePoster();
				floor = 1;
			} else if (floor == 0) {
				engine.stopThread(0);
				freePoster();
				freePosters();
			}
			System.gc();
			System.gc();
			System.gc();
			break;
		case 131:
			if (floor == 1) {
			} else if (floor == 2) {
				engine.getPosterList(String.valueOf(position),
						posterDC.getData());
			}
			break;

		/**
			 * 添加礼品接力部分
			 */
		case 202:
			mld = new MsgListDC();
			break;
			
			
		case MSG_ENTER_IN_END:
			onEnterEnd();
			break;
		case MSG_BACK_OUT_END:
			onBackEnd();
		case MSG_ENTER_SELF_END:
			onEnterSelfEnd();
			break;
		case MSG_BACK_SELF_END:
			onBackSelfEnd();
			break;
		case MSG_ENTER_OUT_END:
			floor = 2;
			break;
		case Configs.STOP_ALL:
			onBackDismissProgressDialogToStopAll();
			break;
		default:
			break;
		}
	}

	private void onBackDismissProgressDialogToStopAll() {
		Log.e("onBackDismissProgressDialogToStopAll",
				"onBackDismissProgressDialogToStopAll");
		engine.stopThread(0);
	}

	private void onBackSelfEnd() {
		Log.e("onBackSelfEnd", "onBackSelfEnd");
		engine.stopThread(0);
		if (flag == 1) {
			if (floor == 2) {
				freePoster();
				floor = 1;
			} else if (floor == 3) {
				floor = 1;
				if (pclDC != null) {
					pclDC.recycle();
				}
				pclDC = null;
			} else if (floor == 4) {
				floor = 2;
				if (pbdc != null) {
					pbdc.recycle();
				}
				pbdc = null;
			}
		} else if (flag == 2) {
			if (floor == 3) {
				pclDC = null;
				floor = 1;
			}
			page = 0;
			onEnterEnd();
			flag = 1;
		}
		System.gc();
		System.gc();
		System.gc();
	}

	private void onEnterSelfEnd() {
		if (!Utils.isNetworkValidate()) {
			Toast.makeText(context, "检测不到可用的网络\n请检查网络连接后重试..", 0).show();
			return;
		}
		if (floor == 2) {
			engine.getPosterList(String.valueOf(position), posterDC.getData());
			posterDC.showDialog();
		} else if (floor == 3) {
			engine.getPosterChanels(pclDC.data);
			pclDC.showDialog();
		} else if (floor == 4) {
			if (pbdc != null) {
				pbdc.showDialog();
				String url = model.getClick_url();
				if (url != null) {
					if (url.endsWith(".jhtml")) {
						url = url + "?sid=" + Configs.TAOBAO_SID_DEFAULT;
					} else {
						url = url + "&sid=" + Configs.TAOBAO_SID_DEFAULT;
					}
					url = url + "&imsi=" + Utils.getIMSI();
					url = url + "&imei=" + Utils.getIMEI();
					url = url + "&ttid=" + Configs.ttid;
					pbdc.loadURL(url);
				} else {
					Toast.makeText(context, "获取登录地址失败", 0).show();
				}
			}
		}
	}

	private void onBackEnd() {
		engine.stopThread(0);
		freePoster();
		freePosters();
		pclDC = null;
		floor = 1;
		model = null;
		System.gc();
		System.gc();
		System.gc();
	}

	private void onEnterEnd() {
		if (!Utils.isSDCard()) {
			return;
		}
		if (!Utils.isNetworkValidate()) {
			this.showNetworkError(postersDC);
			return;
		}
		if (floor == 1) {
			engine.getPostersList(postersDC.getPosters(), ++page);
			postersDC.showDialog();
		}
	}

	public void showNetworkError(BaseDC dc) {
		Toast.makeText(context, "网络连接失败,请检查您的手机网络是否正常或者稍后重试", 1).show();
	}

	void freePoster() {
		engine.stopThread(2);
		if (posterDC != null && posterDC.getData() != null) {
			posterDC.dismissDialog();
			posterDC.mGallery.setDrawingCacheEnabled(false);
			posterDC.mGallery.removeAllViewsInLayout();
			for (int i = 0; i < posterDC.getData().size(); i++) {
				if (posterDC.getData().get(i).getImg() != null) {
					posterDC.getData().get(i).getImg().recycle();
				}
			}
			posterDC.getData().clear();
		}
		posterDC = null;
	}

	void freePosters() {
		engine.stopThread(1);
		if (postersDC != null && postersDC.getData() != null) {
			postersDC.dismissDialog();
			if (postersDC.bmp != null) {
				postersDC.bmp.recycle();
				postersDC.bmp = null;
			}
			postersDC.pGallery.removeAllViewsInLayout();
			for (int i = 0; i < postersDC.getData().size(); i++) {
				for (int j = 0; j < postersDC.getData().get(i).size(); j++) {
					if (postersDC.getData().get(i).get(j).getItemImage() != null) {
						postersDC.getData().get(i).get(j).getItemImage()
								.recycle();
					}
				}
			}
			postersDC.getData().clear();
		}
		postersDC = null;
	}

	@Override
	public boolean back() {
		Log.e("back", "floor:" + floor);
		if (floor == 1) {
			page = 0;
			return true;
		} else if (floor == 2) {
			backSubDC(postersDC);
		} else if (floor == 3) {
			backSubDC(postersDC);
		} else if (floor == 4) {
			backSubDC(posterDC);
		}
		return false;
	}

	@Override
	public BaseDC getMainDC() {
		if (postersDC == null) {
			postersDC = new PostersDC(context,
					Application.getLayoutId(R.layout.posters_list), this);
			pclDC = new PostersChanelsListDC(context,
					Application.getLayoutId(R.layout.posters_chanels), this);
		}
		// return postersDC;
		return pclDC;
	}

	@Override
	public BaseDC getLastDC() {
		if (floor == 2) {
			if (posterDC == null) {
				posterDC = new PosterDC(context,
						Application.getLayoutId(R.layout.poster_list), this);
			}
			return posterDC;
		}
		return getMainDC();
	}

}
